/**
 * Created by LinJe on 2015/10/22.
 */
;(function( $, window ) {

    var olistBox = $('.js-listbox'),
        oLoadMore = $('.js-loadmore'),
        pageStep = 1, totalSize = -1;

    //默认加载
    initLoad();

    //开启页面滚动到底部自动加载更多商品
    $.addScrollToBottom();
    $(window).on('scrollToBottom', function() {
        pageStep ++;
        var opts = {'insertFunction':'append', 'pageStep':pageStep, 'count':20};
        inserProductContent(opts, function( res ) {
            if ( res.scrollEnd ) {
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            } else {
                $.canloadInfo = true;
            }
        });
    });

    //***********公用方法***************
    //初始化加载
    function initLoad() {
        pageStep = 1;
        totalSize = -1;
        oLoadMore.show().removeClass('mtb50').addClass('mtb300').html('<span class="loading"><a><i class="i-05"></i>正在加载更多</a></span>');
        var opts = {
            'insertFunction':'html',
            'pageStep': pageStep,
            'count': 20
        };
        inserProductContent(opts, function( res ) {

            if ( res.scrollEnd ) {
                //oLoadMore.hide();
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            }
        });
    }

    //插入商品内容

    function inserProductContent( opts, callBack ) {
        if ( totalSize != -1 && totalSize < pageStep ) {
            callBack && callBack({'scrollEnd':true});
            return;
        }
        boutiqueServer.getProductData(opts, function( msg ) {
            if ( msg.code == 200 ) {
                //获取数据成功
                olistBox[opts.insertFunction]( getMoreHtml(msg.detail.boutiqueBeans) );
                totalSize = msg.detail.totalSize;
                callBack && callBack({'scrollEnd':totalSize<=1});
            } else {
                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                callBack && callBack({});
            }
        });
    }



    //获取多条商品的html
    function getMoreHtml( data ) {
        var totalHtml = '';

        for ( var i = 0, iLen = data.length; i < iLen; i ++ ) {
            totalHtml += getSingelHtml(data[i]);
        }
        return totalHtml;
    }

    //获取单条商品的html
    function getSingelHtml( data ) {
        return '<div class="list-item-box">\
                  <div class="con-box">\
                      <div class="bd">\
                          <div class="item-1 clearfix">\
                              <div class="img"><div class="center"><img src="'+ data.prodPicUrl +'?imageMogr2/thumbnail/200x200" ></div></div>\
                              <div class="relation">\
                                  <div class="con">\
                                      <h2>'+ data.prodTitle +'</h2>\
                                      <div class="price"><strong>'+ data.prodPrice +'</strong></div>\
                                      <div class="cec"><span class="comment">'+ data.commNum +'</span>，<span class="excerpt">'+ data.excerptNum +'</span>，<span class="channel">'+ data.channelNum +'</span></div>\
                                      <div class="w-opt"><span class="comment"><a href="'+ data.prodUrl +'">进入商品讨论页 &gt;</a></span></div>\
                                  </div>\
                              </div>\
                          </div>\
                          <div class="item-2">\
                              <ul class="clearfix">\
                                  <li class="comment">\
                                      <div class="tnl"><span class="label"># 最热讨论</span>'+(function() {
                return data.commName ? '<span class="user-name"><a href="'+ data.commNameUrl +'">'+ data.commName +'</a></span>' : '';
            })()+'</div>\
                                      '+(function(){
                return data.commComment ? '<div class="con"><div class="text"><p>'+ data.commComment +'</p></div></div>' : '';
            })()+'\
                                  </li>\
                                  <li class="excerpt">\
                                      <div class="tnl"><span class="label"># 最热摘抄</span>'+(function(){
                return data.extractName ? '<span class="user-name"><a href="'+ data.extractNameUrl +'">'+ data.extractName +'</a></span>' : '';
            })()+'</div>\
                                      <div class="con">\
                                          '+(function(){
                return data.extractUrl ? '<div class="link"><a href="'+ data.extractUrl +'" target="_blank">'+ data.extractUrl +'</a></div>' : '';
            })()+'\
                                          '+(function(){
                return data.extractComment ? '<div class="text"><p>'+ data.extractComment +'</p></div>' : '';
            })()+'\
                                      </div>\
                                  </li>\
                                  <li class="channel">\
                                      <div class="tnl"><span class="label"># 最热渠道</span>'+(function(){
                return data.channelName ? '<span class="user-name"><a href="'+ data.channelNameUrl +'">'+ data.channelName +'</a></span>' : '';
            })()+'</div>\
                                      <div class="con">\
                                          '+(function(){
                return data.channelUrl ? '<div class="link"><a href="'+ data.channelUrl +'" target="_blank">'+ data.channelUrl +'</a></div>' : '';
            })()+'\
                                          '+(function(){
                return data.channelComment ? '<div class="text"><p>'+ data.channelComment +'</p></div>' : '';
            })()+'\
                                      </div>\
                                  </li>\
                              </ul>\
                          </div>\
                      </div>\
                  </div>\
                  <div class="bottom"></div>\
              </div>';
    }

})( jQuery, window );